package com.sn.campus.utils;


import com.sn.campus.entity.TBaseSos;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class MsgParse {

    public static TBaseSos parseMessage(String message) {
        TBaseSos deviceMsg = null;
        if (message == null) {
            message = "LD07251515110199000001:TEST*1234#@";
//        "LD07251515110199000001:TEST*1234#@";
//        "LA0726082411019900000101011121G10000*FFFFFF#@";
//        "LD07251515110199000001:STAS*123412121222#@";
//        "LD07260825110199000001:IMEI*1234#@";
//         "LD07260825110199000001:PORT*005056C00008,127.0.0.1,8008,90,7009,8009,91,7010,123456789101,dkfkd#@";
            return deviceMsg;
        }
        Matcher mdatetime = Pattern.compile("L[A-G]{1}([0-9]{8})([a-z0-9A-Z]{12})").matcher(message);
        if (mdatetime.find()) {
            deviceMsg = new TBaseSos();
            deviceMsg.setMsgstr(message);
            deviceMsg.setDatetime(mdatetime.group(1));
            deviceMsg.setDeviceNo(mdatetime.group(2));
            deviceMsg.setProtocolType(message.substring(0, 2));
        } else {
            return null;
        }

        Matcher mtest = Pattern.compile(".*:([a-zA-Z]{1,6})\\*").matcher(message);
        if (mtest.find()) {
            deviceMsg.setProtocolType(deviceMsg.getProtocolType() + mtest.group(1));
        }
        //【LD】+【月日时分】+【设备编号】+【:STAS*】+【信号强度】+【基本1~8防区状态】+【扩展1~8防区状态】+【故障】+【主机】+【延时】+【#@】
        Matcher mstas = Pattern.compile(".*STAS\\*(\\d{2})([A-Fa-f0-9]{2})([A-Fa-f0-9]{2})([A-Fa-f0-9]{2})([A-Fa-f0-9]{2})").matcher(message);
        if (mstas.find()) {
            deviceMsg.setRssi(mstas.group(1));
            Integer x = Integer.parseInt(mstas.group(2), 16);
            deviceMsg.setBasedefencestatus(String.valueOf(Integer.parseInt(mstas.group(2), 16)));
            deviceMsg.setExtenddefencestatus(String.valueOf(Integer.parseInt(mstas.group(3), 16)));
            deviceMsg.setFaultStatus(mstas.group(4));
            deviceMsg.setHost(mstas.group(5));
        }
        //【LD】+【月日时分】+【设备编号】+【:PORT*】+【MAC,12】+【,内网IP】+【,web端口】+【,媒体端口】+【,手机端口】+【,UPNP web端口】+【,UPNP 媒体端口】+【,UPNP 手机端口】+【#@】
        Matcher mport = Pattern.compile(".*PORT\\*([a-z0-9A-Z]{12}),(\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}),(\\d{1,5}),(\\d{1,5}),(\\d{1,5}),(\\d{1,5}),(\\d{1,5}),(\\d{1,5})").matcher(message);
        if (mport.find()) {
            deviceMsg.setMacaddr(mport.group(1));
            deviceMsg.setIntranetip(mport.group(2));
            deviceMsg.setWebport(mport.group(3));
            deviceMsg.setMediaport(mport.group(4));
            deviceMsg.setMobileport(mport.group(5));
            deviceMsg.setUpnpwebport(mport.group(6));
            deviceMsg.setUpnpmediaport(mport.group(7));
            deviceMsg.setUpnpmobileport(mport.group(8));
        }
        //【LA】+【月日时分】+【设备编号】+【模块编号,2位16进制】+【防区编号，2位16进制】
        // +【cid代码，4位第1位1表示事件发生3表示事件回复】+【标识，2位】+【故障状态】+【主机状态】+【*】+【校验码，6位】+【【#@】
        //        "LA0726082411019900000101011121G10000*FFFFFF#@";
        Matcher malarm = Pattern.compile("LA[0-9]{8}[a-z0-9A-Z]{12}([A-Fa-f0-9]{2})([A-Fa-f0-9]{2})([a-zA-Z0-9]{4})([a-zA-Z0-9]{2})([a-zA-Z0-9]{2})([a-zA-Z0-9]{2})\\*").matcher(message);
        if (malarm.find()) {
            deviceMsg.setModelNo(Integer.toBinaryString(Integer.parseInt(malarm.group(1), 16)));
            deviceMsg.setAlarmdefenceno(String.valueOf(Integer.parseInt(malarm.group(2), 16)));
            deviceMsg.setCid(malarm.group(3));
            deviceMsg.setAlarmremark(malarm.group(4));
            deviceMsg.setFaultStatus(malarm.group(5));
            deviceMsg.setHostStatus(malarm.group(6));
        }
        return deviceMsg;
    }
}
